﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{

    public class JLTWB09Data : BaseFunction
    {
        
        public JLTWB09Data()
            : base()
        {

        }
        public JLTWB09Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetWB09(DataRow dwABPOS)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "WB09";
            string ART = dwABPOS["ART"].ToString().Trim();
            string LB = dwABPOS["LB"].ToString().Trim();
            string COLOR = dwABPOS["COLOR"].ToString().Trim();
            string ROLL = dwABPOS["ROLL"].ToString().Trim();
            string STKNR = dwABPOS["STKNR"].ToString().Trim();
            //string FROMDNDATE = dwABPOS["FROMDNDATE"].ToString().Trim();
            //string ENDDNDATE = dwABPOS["ENDDNDATE"].ToString().Trim();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "select a.wbbmandant,a.wbbrwkrs,a.wbbrwlfd,a.wbbpos,ROUND(a.wbbistklg*0.001,2) as WBBISTKLGM,ROUND(a.wbbistklg*0.001/0.9144,2) as WBBISTKLGY, "
                    + " a.wbbart,a.wbbfarbe,to_number(a.wbbpacknr) as wbbpacknr, "
                    + " a.wbbstknr,a.wbblb,a.wbbtax,a.WBBREFSCHL,a.WBBRSFSCHL,a.WBBVEFSCHL,a.WBBVSFSCHL "
                    + " from wbbst a "
                    + " where  A.WBBWAKZ = 'F' "
                    + " AND A.WBBART LIKE '%" + ART + "%' "
                    + " AND (TRIM(A.WBBFARBE) = '" + COLOR + "' OR '" + COLOR + "' = 'AAAAA' ) "
                    + " AND (to_number(A.wbbpacknr) = '" + ROLL + "' OR '" + ROLL + "' = 'AAAA' ) "
                    + " AND (TRIM(A.wbbstknr) = '" + STKNR + "' OR '" + STKNR + "' = 'AAAAAAAAAAAA' ) "
                    + " AND (A.wbblb = '" + LB + "' OR '" + LB + "' = 'AAA' ) "
                    + " ORDER BY WBBFARBE,WBBLB,WBBPACKNR   "
                    ;

                Result = Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    //byte[] b;
                    //string value;
                    //foreach (DataRow dw in Result.Tables[TableName].Rows)
                    //{
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["BEM"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["BEM"] = value;
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["DES"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["DES"] = value;
                    //}
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public void SAVE(DataSet setUI)
        {
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                string MANDANT = "", KRS = "", LFD = "", POS = "", STKNR = "", TAX = "";
                string HAND = "", COLOR1 = "",COLOR2 = "",COLOR3 = "";
                foreach (DataRow dw in setUI.Tables["WB09"].Rows)
                {
                    MANDANT = dw["WBBMANDANT"].ToString();
                    KRS = dw["WBBRWKRS"].ToString();
                    LFD = dw["WBBRWLFD"].ToString();
                    POS = dw["WBBPOS"].ToString();
                    STKNR = dw["WBBSTKNR"].ToString();
                    TAX = Left(dw["WBBTAX"].ToString().Trim().ToUpper() + "   ", 3);
                    HAND = Left(dw["WBBREFSCHL"].ToString().Trim().ToUpper() + "   ", 3);
                    COLOR1 = Left(dw["WBBRSFSCHL"].ToString().Trim().ToUpper() + "   ", 3);
                    COLOR2 = Left(dw["WBBVEFSCHL"].ToString().Trim().ToUpper() + "   ", 3);
                    COLOR3 = Left(dw["WBBVSFSCHL"].ToString().Trim().ToUpper() + "   ", 3);
                    or1.CommandText = "UPDATE WBBST SET "
                        + " WBBTAX = '" + TAX + "', "
                        + " WBBREFSCHL = '" + HAND + "', "
                        + " WBBRSFSCHL = '" + COLOR1 + "', "
                        + " WBBVEFSCHL = '" + COLOR2 + "', "
                        + " WBBVSFSCHL = '" + COLOR3 + "' "
                        + " WHERE WBBMANDANT = '" + MANDANT + "' "
                        + " AND WBBRWKRS = '" + KRS + "' "
                        + " AND WBBRWLFD = '" + LFD + "' "
                        + " AND WBBPOS = '" + POS + "' "
                        + " AND WBBSTKNR = '" + STKNR + "' "
                        ;

                    Connect2Proxy(or1);

                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet BuildJLTWB09Data(DataRow dwUI)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetWB09(dwUI);
                setReturn.Merge(setResult);                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}